package com.senjoeson.socialsecure.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.senjoeson.socialsecure.R;
import com.senjoeson.socialsecure.ui.ListActivity;
import com.senjoeson.socialsecure.ui.PaymentAcitivity;
import com.senjoeson.socialsecure.ui.SocialLoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author senjoeson
 * @date 2018/9/16 22:35
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private int[] homeIcons = new int[]{
            R.mipmap.zhengce,
            R.mipmap.service,
            R.mipmap.comment,
            R.mipmap.formdownload,
            R.mipmap.payment,
            R.mipmap.socialsceue,
    };
    private OnSpeakFunctionListener mOnSpeakTextListener;


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home, parent, false);
        return new ViewHolder(inflate);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mImage.setImageResource(homeIcons[position]);
        holder.mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchUrl(holder.itemView.getContext(), holder.getAdapterPosition());
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String[] stringArray = holder.itemView.getContext().getResources().getStringArray(R.array.home_stringarray);
                mOnSpeakTextListener.speak(stringArray[holder.getAdapterPosition()]);
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return homeIcons.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView mImage;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    private void switchUrl(Context context, int position) {
        Intent intent = new Intent();
        switch (position) {
            case 0:
                intent.setClass(context, ListActivity.class);
                intent.putExtra("loadDataType", 1);
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/2/more_2.html");
                intent.putExtra("title", "政策法规");

                break;
            case 1:
                intent.setClass(context, ListActivity.class);
                intent.putExtra("loadDataType", 1);
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/12/more_12.html");
                intent.putExtra("isService", true);
                intent.putExtra("title", "服务指南");
                break;
            case 2:
                intent.setClass(context, ListActivity.class);
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/mess.html");
                intent.putExtra("loadDataType", 2);
                intent.putExtra("title", "留言回复");
                break;
            case 3:
                intent.setClass(context, ListActivity.class);
                intent.putExtra("loadDataType", 1);
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/download.html");
                intent.putExtra("title", "表格下载");
                break;
            case 4:
                intent.setClass(context, PaymentAcitivity.class);
                intent.putExtra("title", "缴费查询");
                //  intent.putExtra("url", "");
                //  intent.putExtra("title", "缴费查询");
                break;
            case 5:
                intent.setClass(context, SocialLoginActivity.class);
                intent.putExtra("title", "社保登录");
                //  intent.putExtra("url", "");
                //  intent.putExtra("title", "社保登录");
                break;
        }
        context.startActivity(intent);
    }

    public interface OnSpeakFunctionListener {
        void speak(String text);
    }


    public void setOnSpeakTextListener(OnSpeakFunctionListener onSpeakTextListener) {
        if (onSpeakTextListener != null) {
            mOnSpeakTextListener = onSpeakTextListener;
        }
    }

}
